﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace lcs.Web.RecoveryScrap
{
    public partial class Distributionrs : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGV();
            }
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            BindGV();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "lbn_submit")
            {
                if (e.CommandArgument != null)
                {
                    int rsmid = int.Parse(e.CommandArgument.ToString());
                    Maticsoft.BLL.recovery_scrap_material rm = new Maticsoft.BLL.recovery_scrap_material();
                    Maticsoft.Model.recovery_scrap_material r = rm.GetModel(rsmid);

                    GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent));

                    RadioButton rdo_y = drv.FindControl("rdo_y") as RadioButton;//是
                    RadioButton rdo_n = drv.FindControl("rdo_n") as RadioButton;//否

                    TextBox txt_recoveryOrScrapTime = drv.FindControl("txt_recoveryOrScrapTime") as TextBox;//回收报废时间
                    TextBox txt_nums = drv.FindControl("txt_nums") as TextBox;//数量

                    if (string.IsNullOrEmpty(txt_recoveryOrScrapTime.Text))
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请填写时间！');</script>");
                        return;
                    }
                    if (string.IsNullOrEmpty(txt_nums.Text))
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请填写数量！');</script>");
                        return;
                    }

                    if (rdo_y.Checked)
                    {
                        r.status = 2;
                        r.recoveryOrScrapTime = DateTime.Parse(txt_recoveryOrScrapTime.Text);

                        if (rm.Update(r))
                        {
                            BindGV();
                            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('提交成功！');</script>");
                        }
                    }

                    if (rdo_n.Checked)
                    {
                        r.status = 3;
                        r.recoveryOrScrapTime = DateTime.Parse(txt_recoveryOrScrapTime.Text);
                        r.nums = decimal.Parse(txt_nums.Text);

                        if (rm.Update(r))
                        {
                            BindGV();
                            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('提交成功！');</script>");
                        }
                    }
                }
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           
            if (e.Row.RowType == DataControlRowType.DataRow)//判定当前的行是否属于datarow类型的行
            {
                //当鼠标放上去的时候 先保存当前行的背景颜色 并设置新的背景色
                e.Row.Attributes.Add("onmouseover", "onmouseroverop(this)");
                //当鼠标离开的时候 将背景颜色恢复成之前的颜色
                e.Row.Attributes.Add("onmouseout", "onmouseroutop(this)");
                //单击行改变行背景颜色
                e.Row.Attributes.Add("onclick", "ItemOver(this)");
            }
        }

        private void BindGV()
        {
            string str = " 1=1 ";

            if (!string.IsNullOrEmpty(this.txt_proid.Text))
            {
                str += " and proid like '%"+this.txt_proid.Text+"%' ";
            }
            if (!string.IsNullOrEmpty(this.txt_proName.Text))
            {
                str += " and projectName like '%"+this.txt_proName.Text+"%' ";
            }

            str += " and  status = 1 ";

            DataSet ds = new Maticsoft.BLL.recovery_scrap_material().GetListVrsminfo(str);

            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }

        protected void btn_sel_Click(object sender, EventArgs e)
        {
            BindGV();
        }
    }
}